﻿#region Using Statements

using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

using Freeform;
#endregion


namespace Space_in_Shaders
{
    /// <summary>
    /// SpaceObjects are base classes for destroyable ingame assets.
    /// </summary>
    public class SpaceObject : Entity
    {
        public SpaceWorld SpaceWorld { get; set; }
        public Boolean IsDestroyed { get; set; }
        public Int32 Health { get; set; }
        public Int32 Damage { get; set; }
        public Int32 DeathTime { get; set; }
        public RadialCollision Radial { get; set; }
        public new Game Game { get; set; }
        public Actor_Image2D ImageActor { get { return Actor as Actor_Image2D; } }
        public Int32 ExplosionIntensity { get; set; }

        /// <summary>
        /// Creates a new object and automatically links it to the given world.
        /// </summary>
        /// <param name="world">The world that this entity belongs to.</param>
        public SpaceObject(SpaceWorld world) 
            : base(world)
        {
            Game = world.Game;
            SpaceWorld = world;
            IsDestroyed = false;
            Health = 1;
            Damage = 0;
            DeathTime = 0;

            // Setup collision properties for the spaceship.
            CollisionProperties.CollisionID = RadialCollision.CID_GENERIC;
            CollisionProperties.CollisionType = CollisionType.None;
            CollisionProperties.CollisionOccured += HandleCollision;
            Radial = new RadialCollision(this, 0);
        }


        /// <summary>
        /// Destroys the object and removes it from the world with an available override.
        /// </summary>
        public void Destroy()
        {
            IsDestroyed = true;
            OnDestroy();
        }


        public virtual void OnDestroy() 
        {
            if (ExplosionIntensity > 0)
            {
                Vector2 Vel = Vector2.Zero;
                if (SpatialProperties.MotionController != null && SpatialProperties.MotionController as NMotion != null)
                    Vel = (SpatialProperties.MotionController as NMotion).Velocity;
                Explosion.Create(SpaceWorld, SpatialProperties.Position, Vector2.Zero, 3 * ExplosionIntensity, ExplosionIntensity, 15 * ExplosionIntensity);
            }
        }


        /// <summary>
        /// Allows the object to update it's state.
        /// </summary>
        /// <param name="time">Provides a snapshot of timing values.</param>
        public override void Update(GameTime time)
        {
            base.Update(time);

            if (Health <= 0)
                Destroy();

            if (IsDestroyed)
            {
                DeathTime -= time.ElapsedGameTime.Milliseconds;
                if (DeathTime <= 0)
                    Dispose();
            }
        }


        private void HandleCollision(Entity A, Entity B)
        {
            SpaceObject AObj = (SpaceObject)A;
            SpaceObject BObj = (SpaceObject)B;
            if (AObj == null || BObj == null)
                return;

            AObj.CollidesWith(BObj);
            BObj.CollidesWith(AObj);
        }


        public virtual void CollidesWith(SpaceObject Other)
        {
            Health -= Other.Damage;
        }
    }
}